package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查项管理
 * Author chongge
 * Time 2019/10/25 0:46
 * ClassName com.itheima.controller
 */
@RestController
@RequestMapping(value = "/menu")
public class MenuController {

    //通过dubbo调用service服务
    @Reference
    private MenuService menuService;

    /***
     * 新增检查项
     */
    @PreAuthorize("hasAuthority('MENU_ADD')")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /***
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    @RequestMapping(value = "/findPage",method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = null;
        try {
           pageResult = menuService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }


    /***
     * 根据检查项id删除检查项
     * @param id
     * @return
     */
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result deleteById(Integer id){
        PageResult pageResult = null;
        try {
            menuService.deleteById(id);
        }catch (RuntimeException e){

            return new Result(false,e.getMessage());

        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }

        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 根据检查项的Id来查询检查项数据
     * @param id
     * @return
     */
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(Integer id){
        Menu menu = null;
        try {
             menu = menuService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,menu);
    }


    /***
     * 更新检查项
     * @param
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Result edit(@RequestBody Menu menu){
        System.out.println(menu);
        try {
            menuService.edit(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }


    /***
     *
     * 查询所有检查项数据
     *
     * @return
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll(){
        try {
            List<Menu> MenuList = menuService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,MenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }



}
